package com.ecoolex.discount.sales.ws;

import java.sql.Date;
import java.time.Instant;
import java.util.Objects;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.PropertyMapper;

import com.ecoolex.discount.sales.core.manager.YWCtripOrderManager;
import com.ecoolex.discount.sales.entity.YWCtripOrder;
import com.ecoolex.discount.sales.pojo.GenerateCtripOrderDTO;

@Service
public class YWCtripOrderServiceImpl implements YWCtripOrderService {
	@Autowired
	YWCtripOrderManager ywCtripOrderManager;

	@Override
	public YWCtripOrder getOrderByOrderNo(String orderNo) {
		YWCtripOrder order = ywCtripOrderManager.getOneByOrderNo(orderNo);
		return order;
	}

	@Override
	public YWCtripOrder updateStatus(YWCtripOrder ywCtripOrder) {
		return ywCtripOrderManager.updateStatus(ywCtripOrder);
	}

	@Override
	public YWCtripOrder save(GenerateCtripOrderDTO generateCtripOrderDTO) {
		YWCtripOrder order = getOrderByOrderNo(generateCtripOrderDTO.getOrderNo());
		if (Objects.nonNull(order)) {
			return null;
		}
		else {
			YWCtripOrder toBeSave = new YWCtripOrder();
			PropertyMapper mapper = PropertyMapper.get();
			mapper.from(generateCtripOrderDTO::getChannelId).to(toBeSave::setChannelId);
			mapper.from(generateCtripOrderDTO::getServiceId).to(toBeSave::setServiceId);
			mapper.from(generateCtripOrderDTO::getUserId).to(toBeSave::setUserId);
			mapper.from(generateCtripOrderDTO::getPhone).to(toBeSave::setPhone);
			mapper.from(generateCtripOrderDTO::getOrderNo).to(toBeSave::setOrderNo);
			mapper.from(generateCtripOrderDTO::getCurrency).to(toBeSave::setCurrency);
			mapper.from(generateCtripOrderDTO::getStatus).to(toBeSave::setStatus);
			mapper.from(generateCtripOrderDTO::getPaymentTime).to(toBeSave::setPaymentTime);
			mapper.from(generateCtripOrderDTO::getPaymentNo).to(toBeSave::setPaymentNo);
			mapper.from(generateCtripOrderDTO::getRefundAmount).to(toBeSave::setRefundAmount);
			mapper.from(generateCtripOrderDTO::getCreateTime).to(toBeSave::setOrderTime);
			mapper.from(generateCtripOrderDTO::getCardId).to(toBeSave::setCardId);
			mapper.from(generateCtripOrderDTO::getBuyNumber).to(toBeSave::setBuyNumber);
			mapper.from(generateCtripOrderDTO::getCounterPrice).to(toBeSave::setCounterPrice);
			mapper.from(generateCtripOrderDTO::getRealPrice).to(toBeSave::setRealPrice);
			mapper.from(generateCtripOrderDTO::getCardDiscountPercent).to(toBeSave::setCardDiscountPercent);
			mapper.from(generateCtripOrderDTO::getDiscountPrice).to(toBeSave::setDiscountPrice);
			mapper.from(generateCtripOrderDTO::getImage).to(toBeSave::setImage);
			toBeSave.setDeleted(YWCtripOrder.NOT_DELETED);
			toBeSave.setCreateTime(Date.from(Instant.now()));
			toBeSave.setCreateUser("system-admin");
			toBeSave.setUpdateUser("system-admin");
			toBeSave.setUpdateTime(Date.from(Instant.now()));
			toBeSave = ywCtripOrderManager.save(toBeSave);
			return toBeSave;
		}
	}
}
